package com.hk.juc.util;


import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;


/**
 * 当我们调用CountDownLatch的countDown方法时，N就会减1，CountDownLatch的await方法
 * 会阻塞当前线程，直到N变成零。由于countDown方法可以用在任何地方，所以这里说的N个
 * 点，可以是N个线程，也可以是1个线程里的N个执行步骤。用在多个线程时，只需要把这个
 * CountDownLatch的引用传递到线程里即可
 */
@Slf4j
public class CountDownLatchTest {

    public static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {

        new Thread(() -> {
            try {
                Thread.sleep(2*1000);
                log.info("线程1执行完毕~~~");
                countDownLatch.countDown();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                Thread.sleep(2*1000);
                log.info("线程2执行完毕~~~");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        countDownLatch.await();

        log.info("运行结束~");
    }


}
